<template>
    <div class="container containerAdmin">
        <!-- header -->
        <div class="header">
            <div class="header-boxTime">
                <p
                    class="header-data"
                    v-for="(item,index) in weekDate"
                    :key="index"
                >
                    <span
                        :class="index == currentIndex ? 'header-current-time' : 'header-time' "
                        @click="fnGetData(item,index)"
                    >{{ item | setDate }}</span>
                </p>
            </div>
            <div class="header-box">
                <p class="header-title">秒杀状态:</p>
                <el-select
                    v-model="examine"
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="fnGetExamine()"
                >
                    <el-option
                        v-for="item in examineList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <p class="header-title">查询秒杀时间:</p>
                <el-select
                    v-model="time"
                    placeholder="选择秒杀时间"
                    size="small"
                    @change="getTableData()"
                >
                    <el-option
                        v-for="(item,index) in scekillList"
                        :key="index"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="header-box">
                <el-popover
                    placement="top-start"
                    width="200"
                    trigger="hover"
                    content="当场的结束时间需和下一场的开始时间一样">
                    <el-button
                        slot="reference"
                        type="primary"
                        size="small"
                        @click="dialogVisible = true "
                    >创建秒杀时间<i class="el-icon-info"/></el-button>
                </el-popover>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <div class="table-list">
            <div
                v-for="(item,index) in tableData"
                :key="index"
            >
                <div class="table-header">
                    <p class="el-table-time">{{ scekillTime | getDay }}</p>
                    <div class="sort-wrap">
                        {{ item.start_time * 1000 | dateFormatter('HH:mm') }} - {{ item.end_time * 1000 | dateFormatter('HH:mm') }}
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(item)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                        <p
                            class="sort-time"
                            @click="fnDelDate(item)"
                        >删除</p>
                    </div>
                </div>
                <el-table
                    :data="item.newArr"
                    class="tabledata"
                    style="width: 100%;"
                    border
                >
                    <el-table-column
                        prop="goods_title"
                        label="商品名称"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="goods_cover"
                        min-width="110"
                        label="商品缩略图"
                    >
                        <template slot-scope="scope">
                            <img
                                :src="scope.row.goods_cover"
                                class="img-cover"
                            >
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_type"
                        label="商品类型"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.goods_type == 2">淘客商品</span>
                            <span v-else-if="scope.row.goods_type == 3">自营秒杀</span>
                            <span v-else>--</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_price"
                        label="秒杀价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="goods_price_origin"
                        label="原价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="sort"
                        show-overflow-tooltip
                        label="排序"
                    >
                        <template slot-scope="scope">
                            <div class="sort-wrap">
                                {{ scope.row.sort }}
                                <span class="sort-spliter" />
                                <el-button
                                    type="text"
                                    class="btn-sort"
                                    @click="fnSeckillSort(scope.row)"
                                >
                                    <i class="el-icon-edit" />
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_num"
                        label="开始库存"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span
                            >{{ scope.row.goods_num }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_num"
                        label="剩余库存"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span
                            >{{ scope.row.goods_num - scope.row.sale_num }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="sale_num"
                        label="真实销量"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span>{{ scope.row.sale_num }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="sale_num"
                        label="APP显示销量"
                        :width="130"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span>{{ scope.row.sale_num + scope.row.sale_num_auto }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="view_count"
                        label="真实浏览量"
                        show-overflow-tooltip
                        :width="120"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span>{{ scope.row.view_count }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="view_count_auto"
                        label="企业主浏览量"
                        :width="130"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span v-if=" scope.row.goods_type == 3 ">{{ scope.row.view_count_auto + scope.row.view_count }}</span>
                            <span v-if=" scope.row.goods_type == 2 ">--</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_buy_limit_count"
                        label="限制购买数量"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        :width="140"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.goods_buy_limit_count !== -1 ">{{ scope.row.goods_buy_limit_count }}件</span>
                            <span v-if=" scope.row.goods_buy_limit_count == -1 ">不限制</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="status"
                        label="秒杀状态"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.status == 1">未开始</span>
                            <span v-if="scope.row.status == 2">进行中</span>
                            <span v-if="scope.row.status == 3">已结束</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        :width="130"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                class="curse-add"
                                style="marginLeft:0;padding:5px 0 5px 0"
                                @click="fnGetLookShop(scope.row, scope.row.goods_type)"
                            >销量自动增加</el-button>
                            <el-button
                                type="text"
                                class="curse-add"
                                style="marginLeft:0;padding:0"
                                @click="fnGetAddShop(scope.row)"
                                v-if="scope.row.goods_type == 3"
                            >浏览量自动增加</el-button>
                            <el-button
                                type="text"
                                class="curse-add"
                                style="marginLeft:0;padding:0"
                                @click="fnSetShopNum(scope.row)"
                            >设定购买数量</el-button>
                            <el-button
                                type="text"
                                style="marginLeft:0;padding:0"
                                :disabled="nowTime >= scekillTime"
                                @click="fnGetRemoveShop(scope.row)"
                            >移除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <!-- /table -->

        <!-- 无数据 -->
        <div
            class="empty-wrap"
            v-if="tableData.length <= 0 && isData "
        >
            <div class="empty-img" />
            <p class="empty-text">
                暂时还没有数据哦～
            </p>
        </div>
        <!-- 无数据 -->

        <!-- 设定购买数量 -->
        <el-dialog
            title="设定购买数量"
            :visible.sync="dialogVisiblePurchase"
            width="400px"
            center
        >
            <el-input-number
                v-model="purchase"
                :min="1"
                size="small"
                style="marginLeft:108px"
            />
            <span style="paddingLeft:5px">件</span>
            <span
                slot="footer"
                class="dialog-footer">
                <el-button
                    @click="dialogVisiblePurchase = false"
                    size="small"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="fnConfirmPurchase()">确 定</el-button>
            </span>
        </el-dialog>
        <!-- /设定购买数量 -->

        <!-- 创建秒杀时间 -->
        <el-dialog
            title="创建秒杀时间"
            :visible.sync="dialogVisible"
            width="510px"
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="选择日期"
                    prop="scekillData"
                >
                    <el-date-picker
                        v-model="ruleForm.scekillData"
                        type="date"
                        size="small"
                        placeholder="选择秒杀日期"
                        @change="fnGetSeckillData"
                    />
                </el-form-item>
                <el-form-item
                    label="选择时间"
                    prop="scekillTime"
                >
                    <el-time-picker
                        is-range
                        v-model="ruleForm.scekillTime"
                        range-separator="至"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        placeholder="选择时间范围"
                        format='HH:mm'
                        :disabled="isDate"
                        :clearable="false"
                        size="small"
                        @change="fnGetSeckillTime"
                    />
                </el-form-item>
                <p
                    class="el-seckill"
                    v-if="createTimeList.length > 0"
                >
                    <el-tag
                        v-for="(tag,index) in createTimeList"
                        :key="index"
                        closable
                        @close="handleClose(tag,index)"
                    >
                        {{ tag.label }}
                    </el-tag>
                </p>
                <el-form-item>
                    <el-button
                        size="small"
                        class="scekillrel"
                        @click="resetForm('ruleForm')">取消</el-button>
                    <el-button
                        type="primary"
                        size="small"
                        class="scekillbtn"
                        @click="submitForm('ruleForm')">确定</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!-- /创建秒杀时间 -->

        <!-- 销量自动增加 -->
        <el-dialog
            title="库存扣除量"
            width="700px"
            :visible.sync="dialogVisibleDeduction"
            :close-on-click-modal="false"
        >

            <el-form
                :model="ruleFormDeduction"
                ref="ruleFormDeduction"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="销量"
                    v-if="isTk"
                >
                    <el-input
                        v-model="tao_item_stock_num"
                        placeholder="请输入增加销量"
                        size="small"
                    />
                </el-form-item>
                <el-form-item
                    v-if="!isTk"
                >
                    <div
                        class="el-deduction-list"
                        v-for="(item,index) in DeductionList"
                        :key="index"
                    >
                        <el-input
                            v-model="ruleFormDeduction[index]"
                            class="el-deduction-ipt"
                            size="small"
                            type="number"
                            min="0"
                            :max="item.product_stock"
                        />
                        <div
                            v-for="(itm,idx) in item.spec_list"
                            :key="idx"
                            class="el-max-width"
                        >
                            <el-popover
                                placement="top-start"
                                width="200"
                                trigger="hover"
                                :content="itm.spec_name + ':' +itm.spec_value + '/' + item.product_stock + '件'"
                            >
                                <el-button
                                    slot="reference"
                                    class="el-provide-text"
                                >
                                    {{ itm.spec_name }}:{{ itm.spec_value }} / {{ item.product_stock }}件
                                </el-button>
                            </el-popover>
                        </div>
                    </div>
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    class="el-deductionSave"
                    @click="submitFormDeduction()">保存</el-button>
            </el-form>
        </el-dialog>
        <!-- /销量自动增加 -->

        <!-- 编辑限时抢购 -->
        <el-dialog
            :visible.sync="dialogVisibleEdit"
            width="410px"
            center
            title="编辑场次时间"
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleFormEdit"
                ref="ruleFormEdit"
                :rules="rulesEdit"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="开始时间"
                    prop="startTime"
                >
                    <el-time-select
                        placeholder="起始时间"
                        v-model="ruleFormEdit.startTime"
                        size="small"
                        :picker-options="{
                            start: '00:00',
                            step: '00:15',
                            end: '24:00'
                        }"
                    />
                </el-form-item>
                <el-form-item
                    label="结束时间"
                    prop="editTime"
                >
                    <el-time-select
                        placeholder="结束时间"
                        v-model="ruleFormEdit.editTime"
                        size="small"
                        :picker-options="{
                            start: '00:00',
                            step: '00:15',
                            end: '24:00',
                            minTime: ruleFormEdit.startTime
                        }"
                    />
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    class="el-deductionSave"
                    @click="submitFormEdit('ruleFormEdit')">保存</el-button>
            </el-form>
        </el-dialog>
        <!-- /编辑限时抢购 -->

        <!-- 浏览量自动增加 -->
        <el-dialog
            :visible.sync="dialogVisibleAdd"
            width="410px"
            title="浏览量增长规则"
            center
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleFormAdd"
                ref="ruleFormAdd"
                label-width="30px"
                class="demo-ruleForm"
            >
                <el-form-item>
                    <div class="el-deduction-list">
                        <el-input
                            v-model="ruleFormAdd.num"
                            class="el-deduction-ipt"
                            size="small"
                            type="number"
                            min="0"
                        />
                        <p :style="{'padding':'0 3px'}">个/</p>
                        <el-input
                            v-model="ruleFormAdd.time"
                            class="el-deduction-ipt"
                            size="small"
                            type="number"
                            min="0"
                        />
                        <p :style="{'marginLeft': '3px'}">分钟</p>
                    </div>
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    class="el-deductionSave"
                    @click="submitFormAdd('ruleFormAdd')">保存</el-button>
            </el-form>
        </el-dialog>
        <!-- /浏览量自动增加 -->
    </div>
</template>

<script>
/**
 * @param {String}  nowTime //当前日期时间戳
 * @param {String}  time //秒杀时间
 * @param {Array}   scekillList //秒杀时间数据
 * @param {Array}   tableData //表单数据
 * @param {Boolean} dialogVisible //弹窗状态
 * @param {Object}  ruleForm //弹窗表单字段
 * @param {Object}  rules //弹窗表单验证
 * @param {Array}   createTimeList //创建时间数据
 * @param {Array}   weekDate //最近一周日期
 * @param {Number}  currentIndex //默认选中样式
 * @param {string}  examine //秒杀状态
 * @param {Array}   examineList //秒杀状态数据
 * @param {Boolean} dialogVisibleDeduction //销量自动增加弹窗开关
 * @param {Array}   DeductionList //销量自动增加弹窗数据
 * @param {Object}  ruleFormDeduction //销量自动增加弹窗表单数据
 * @param {Boolean} dialogVisibleAdd //浏览量自动增加弹窗开关
 * @param {Object}  ruleFormAdd //浏览量自动增加弹窗表单字段
 * @param {String}  sale_no //抢购编号
 * @param {String}  time_no //场次编号
 * @param {Array}   tableDataCopy //用于数据筛选
 * @param {Boolean} dialogVisibleEdit  //编辑场次弹窗开关
 * @param {Boolean} isDate  //是否选择时期
 */
let self = this;

export default {
    data () {
        return {
            isTk: true,
            tao_item_stock_num: '',
            purchase: 1,
            dialogVisiblePurchase: false,
            time_no: '',
            sale_no: '',
            nowTime: this.fnGetDateTime('','date'),
            time: '',
            scekillTime: this.fnGetDateTime('','date'),
            scekillList: [],
            weekDate: [],
            currentIndex: 3,
            dialogVisible: false,
            examine: '',
            isData: false,
            dialogVisibleEdit: false,
            isDate: true,
            ruleFormEdit: {
                startTime: '',
                editTime: ''
            },
            rulesEdit: {
                startTime: [
                    { required: true, message: '请选择时间,不可以为空', trigger: 'blur' }
                ],
                editTime: [
                    { required: true, message: '请选择时间,不可以为空', trigger: 'blur' }
                ]
            },
            examineList: [{
                value: '',
                label: '全部'
            },{
                value: 1,
                label: '未开始'
            },{
                value: 2,
                label: '进行中'
            },{
                value: 3,
                label: '已结束'
            }],
            dialogVisibleDeduction: false,
            ruleFormDeduction: {

            },
            DeductionList: [],
            dialogVisibleAdd: false,
            ruleFormAdd: {
                num: '',
                time: ''
            },
            ruleForm: {
                scekillData: '',
                scekillTime: ''
            },
            rules: {
                scekillData: [
                    { required: true, message: '请选择秒杀日期,不可以为空', trigger: 'blur' }
                ],
                scekillTime: [
                    { required: true, message: '请选择秒杀时间,不可以为空', trigger: 'blur' }
                ]
            },
            createTimeList: [],
            tableData: [],
            tableDataCopy: [],
            indexSucess: 0,
            indexError: 0
        };
    },
    beforeCreate: function () {
        self = this;
    },
    created () {
        this.setHeader();
        this.getTableData();
        this.fnGetScekillTimeList();
        this.fnGetWeekTime();
    },
    watch: {
        //弹窗数据清空
        dialogVisibleDeduction (val) {
            if (!val) {
                this.ruleFormDeduction = {};
                this.DeductionList = [];
            }
        },
        dialogVisible (val) {
            if (!val) {
                this.ruleForm = {
                    scekillData: '',
                    scekillTime: ''
                };
                this.createTimeList = [];
                this.isDate = true;
            }
        },
        dialogVisibleAdd (val) {
            if (!val) {
                this.ruleFormAdd = {
                    num: '',
                    time: ''
                };
            }
        },
        dialogVisibleEdit (val) {
            this.ruleFormEdit = {
                startTime: '',
                editTime: ''
            };
        },
        dialogVisiblePurchase (val) {
            if (!val) {
                this.purchase = 1;
            }
        }
    },
    filters: {
        setDate (val) {
            if (val.value == self.nowTime - 86400000) {
                return '昨天';
            } else if (val.value == self.nowTime) {
                return '今天';
            } else if (val.value == self.nowTime + 86400000) {
                return '明天';
            } else {
                return val.label;
            }
        },
        getDay (val) {
            if (val == self.nowTime - 86400000) {
                return '昨天';
            } else if (val == self.nowTime) {
                return '今天';
            } else if (val == self.nowTime + 86400000) {
                return '明天';
            } else {
                let year = new Date(val).getFullYear(),
                    month = new Date(val).getMonth() + 1,
                    day = new Date(val).getDate();

                return year + '-' + month + '-' + day;
            }
        }
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{title: "APP后台",},{title: '商城'},{title: '秒杀'},{title: "秒杀管理"}]);
        },

        /**
         * 获取表格数据
         */
        getTableData () {
            this.$post('/student/shop_goods_flash_sale%5Cget_list', {
                day_time: this.scekillTime / 1000,
                time_no: this.time
            }, resp => {
                if (resp.code == 1) {
                    let arr = [],
                        data = [],
                        newArr = [];

                    data.push(resp.data);
                    data.map(item=> {
                        item.time_list.map((list,index)=>{
                            arr.push({
                                start_time: list.start_time,
                                end_time: list.end_time,
                                time_no: list.time_no,
                                newArr: ''
                            });
                            for(let i in item.time_goods_map) {

                                if (i == list.time_no ) {
                                    arr[index].newArr = item.time_goods_map[i];
                                    /* 商品数据为空处理 */
                                    // if (JSON.stringify(item.time_goods_map[i]) !== '[]') {
                                    //     arr[index].newArr = item.time_goods_map[i];
                                    // }
                                }
                            }
                            return;
                        });
                    });

                    arr.map((item,index)=>{
                        if (item.newArr !== '') {
                            newArr.push(item);
                        }
                        return;
                    });

                    this.tableDataCopy = this.tableData = newArr;
                    this.isData = true;
                }
            });
        },

        /**
         * 销量自动增加
         */
        fnGetLookShop (row, number) {
            if (number == 3) {
                this.isTk = false;
            } else {
                this.tao_item_stock_num = row.sale_auto_config['tao_item_stock_num'];
                this.isTk = true;
            }

            let specList = row.sale_auto_config.product;

            this.sale_no = row.sale_no;
            this.$post('/student/shop_goods%5Cget_product',{
                goods_no: row.goods_no
            }, resp=> {
                if (resp.code == 1) {
                    this.DeductionList = resp.data.goods_product_list;
                    if (JSON.stringify(specList) !== '[]') { //需要回填数据
                        this.DeductionList.map((item,index)=> {
                            if (JSON.stringify(item.spec_list) == JSON.stringify(specList[index].specs) ) {
                                this.ruleFormDeduction[Number(index)] = specList[index].num;
                            }
                        });
                        this.ruleFormDeduction = JSON.parse(JSON.stringify(this.ruleFormDeduction));
                    } else {
                        this.DeductionList.map((item,index)=>{
                            this.ruleFormDeduction[index] = 0;
                        });
                        this.ruleFormDeduction = JSON.parse(JSON.stringify(this.ruleFormDeduction));
                        return false;
                    }
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
            this.dialogVisibleDeduction = !this.dialogVisibleDeduction;
        },

        /**
         * 设定购买数量
         */
        fnSetShopNum (row) {
            this.product = row.goods_buy_limit_count;
            this.goods_no = row.goods_no;
            this.dialogVisiblePurchase = !this.dialogVisibleDeduction;
        },

        /**
         * 设定购买数量
         */
        fnConfirmPurchase () {
            this.$post('/student/shop_goods%5Cset_buy_limit_count',{
                goods_no: this.goods_no,
                buy_limit_count:  this.purchase
            },resp=>{
                if (resp.code == 1) {
                    this.getTableData();
                    this.dialogVisiblePurchase = !this.dialogVisiblePurchase;
                    this.$message({
                        type: 'success',
                        message: '设置成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 保存库存扣除量
         */
        submitFormDeduction () {
            let arr = [];
            let value = this.ruleFormDeduction;

            /* 表单项为空时不允许提交 */
            for(let i in value) {
                if (value[i] === '') {
                    this.$message({
                        type: 'waring',
                        message: '表单选项不可以为空,不扣除时请填写0'
                    });
                    return;
                }
            }
            /* 表单项为空时不允许提交 */

            this.DeductionList.map((item,index)=>{
                arr.push({
                    specs: item.spec_list,
                    num: this.ruleFormDeduction[index]
                });
                return;
            });

            this.$post('/student/shop_goods_flash_sale%5Cset_sale_auto',{
                sale_no: this.sale_no,
                status: 1,
                product_config: JSON.stringify(arr),
                tao_item_stock_num: this.isTk ? this.tao_item_stock_num : ''
            }, resp=> {
                if (resp.code == 1) {
                    this.getTableData();
                    this.dialogVisibleDeduction = !this.dialogVisibleDeduction;
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 浏览量自动增加
         */
        fnGetAddShop (row) {
            this.sale_no = row.sale_no;
            this.ruleFormAdd = {
                num: row.view_auto_config.num,
                time: row.view_auto_config.second
            };
            this.dialogVisibleAdd = !this.dialogVisibleAdd;
        },

        /**
         * 浏览量自动增加保存
         */
        submitFormAdd (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$post('/student/shop_goods_flash_sale%5Cset_view_auto',{
                        sale_no: this.sale_no,
                        status: 1,
                        num: this.ruleFormAdd.num,
                        second: this.ruleFormAdd.time * 60
                    }, resp=> {
                        if (resp.code == 1) {
                            this.getTableData();
                            this.dialogVisibleAdd = !this.dialogVisibleAdd;
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            });
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'waring',
                                message: resp.msg
                            });
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 移除
         */
        fnGetRemoveShop (row) {
            this.$confirm('此操作将移除该商品, 是否继续?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_goods_flash_sale%5Cdelete',{
                    sale_no: row.sale_no
                }, resp=> {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '移除成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'waring',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 获取最近一周中排期商品数据
         */
        fnGetData (item,index) {
            this.currentIndex = index;
            this.scekillTime = item.value;
            this.time = '';
            this.getTableData();
            this.fnGetScekillTimeList();
        },

        /**
         * 时间戳转时+分
         */
        getHous (time) {
            let Hous = new Date(time * 1000).getHours(),
                Minus = new Date(time * 1000).getMinutes(),
                Sce = new Date(time * 1000).getSeconds();

            return this.setDate(Hous) + ':' + this.setDate(Minus) + ':' + this.setDate(Sce);
        },

        /**
         * 获取秒杀时间点列表
         */
        fnGetScekillTimeList () {
            this.scekillList = [];
            this.$post('/student/shop_goods_flash_sale%5Cget_simple_time_list', {
                day_time: this.scekillTime / 1000
            }, resp=> {
                if (resp.code == 1) {
                    resp.data.list.map(item=>{
                        this.scekillList.push({
                            value: item.time_no,
                            label: this.getHous(item.start_time) + ' - ' + this.getHous(item.end_time)
                        });
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 秒杀状态数据筛选
         */
        fnGetExamine () {
            let tableData = [];

            if (this.examine == '') {
                this.tableData = this.tableDataCopy;
                return;
            }

            this.tableDataCopy.map(item=>{
                item.newArr.map(itm=>{
                    if (itm.status == this.examine) {
                        tableData.push(item);
                    }
                });
            });

            tableData = tableData.filter(function(element,index,self){
                return self.findIndex(el=>el.newArr==element.newArr)=== index;
            });

            this.tableData = tableData;
        },

        /**
         * 获取日期时间戳
         */
        fnGetDateTime (time, date) {
            let scep = null;

            if (time == '') {
                scep = new Date().getTime();
            } else {
                scep = time;
            }

            if (date == 'date') {
                let year = new Date(scep).getFullYear(),
                    month = new Date(scep).getMonth()+1,
                    day = new Date(scep).getDate();
                let data = this.setDate(year) + '-' + this.setDate(month) + '-' + this.setDate(day);
                let nowTime = new Date(data).getTime();

                return nowTime;
            } else if (date == 'isScpe') {
                let year = new Date(scep).getFullYear(),
                    month = new Date(scep).getMonth()+1,
                    day = new Date(scep).getDate(),
                    House = new Date(scep).getHours(),
                    Minus = new Date(scep).getMinutes();
                let data = this.setDate(year) + '-' + this.setDate(month) + '-' + this.setDate(day) + ' ' + this.setDate(House) + ':' + this.setDate(Minus) + ':00';

                let nowTime = new Date(data).getTime();

                return nowTime;
            }
        },

        /**
         *  日期补0
         */
        setDate (param) {
            return param < 10 ? '0' + param : param;
        },

        /**
         * 编辑时间/今天之后的时间点才可编辑
         */
        fnEditSort (item) {
            let dayTime = this.fnGetDateTime('','date');

            if (this.scekillTime > dayTime) {
                this.time_no = item.time_no;
                this.dialogVisibleEdit = !this.dialogVisibleEdit;
            } else {
                this.$message({
                    type: 'waring',
                    message: '当天之后的时间点才可已编辑'
                });
            }
        },

        /**
         * 保存场次修改时间
         */
        submitFormEdit (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    let year = new Date(this.scekillTime).getFullYear(),
                        month = new Date(this.scekillTime).getMonth() +1,
                        day = new Date(this.scekillTime).getDate();

                    let startDate = year + '-' + this.setDate(month) + '-' + this.setDate(day) + ' ' + this.ruleFormEdit.startTime + ':00',
                        endDate = year + '-' + this.setDate(month) + '-' + this.setDate(day) + ' ' + this.ruleFormEdit.editTime + ':00';

                    this.$post('/student/shop_goods_flash_sale%5Cset_time',{
                        time_no: this.time_no,
                        start_time: new Date(startDate).getTime() / 1000,
                        end_time: new Date(endDate).getTime() / 1000,
                    },resp=> {
                        if (resp.code == 1) {
                            this.dialogVisibleEdit = !this.dialogVisibleEdit;
                            this.getTableData();
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            });
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'waring',
                                message: resp.msg
                            });
                        }
                    });
                } else {
                    return false;
                }
            });
        },

        /**
         * 排序修改
         */
        fnSeckillSort (row) {
            this.$prompt('修改', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPlaceholder: '请输入数字',
                inputErrorMessage: '请输入正整数',
                inputValue: row.sort,
                closeOnClickModal: false,
            }).then(({ value }) => {
                this.$post('/student/shop_goods_flash_sale%5Cset_sort', {
                    sale_no: row.sale_no,
                    sort: value
                }, resp => {
                    if (resp.code == 1) {
                        this.$message({
                            type: 'success',
                            message: '修改成功'
                        });
                        this.$set(row, 'sort', value);
                    } else {
                        this.$message({
                            type: 'warning',
                            messaeg: resp.msg || '修改失败'
                        });
                    }
                });
            });
        },

        /**
         * 删除秒杀时间点
         */
        fnDelDate (item) {
            let dayTime = this.fnGetDateTime('','date');
            let isDel = JSON.stringify(item.newArr); //当前场次是否有数据_删除条件

            if (isDel == '[]') {
                this.fnDeleTimeNo(item.time_no);
                return;
            }

            if (this.scekillTime <= dayTime) {
                this.$message({
                    type: 'waring',
                    message: '当前场次已添加商品,暂不支持删除'
                });
                return;
            } else {
                this.fnDeleTimeNo(item.time_no);
            }
        },

        fnDeleTimeNo (time_no) {
            this.$confirm('是否要删除当前排期', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_goods_flash_sale%5Cdelete_time', {
                    time_no: time_no
                }, resp=> {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'waring',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 获取最近一周日期/系统时间
         */
        fnGetWeekTime () {
            let newDate = [];

            for(let i = this.nowTime - 86400000 * 3; i <= this.nowTime + 86400000 * 3; i+= 86400000 ) {
                let weekdate = new Date(i).getFullYear() + '年' + (new Date(i).getMonth()+1) + '月' + new Date(i).getDate() + '日';

                newDate.push({
                    value: i,
                    label: weekdate
                });
            }
            this.weekDate = newDate;
        },

        /**
         * 选择秒杀日期
         */
        fnGetSeckillData () {
            let nowTime = this.fnGetDateTime('', 'date');

            this.ruleForm.scekillData = this.fnGetDateTime(this.ruleForm.scekillData, 'date');
            this.isDate = false;

            if (this.ruleForm.scekillData < nowTime) {
                this.$refs['ruleForm'].resetFields();
                this.$message({
                    type: 'waring',
                    message: '请选择今天之后的日期'
                });
                return;
            }
        },

        /**
         * 选择秒杀时间/系统时间
         */
        fnGetSeckillTime () {
            let nowTime = this.fnGetDateTime('', 'date');
            let start =Number(this.fnGetDateTime(this.ruleForm.scekillTime[0], 'isScpe')) + Number(this.fnGetDateTime(this.ruleForm.scekillData, 'date')) - nowTime,
                ent = Number(this.fnGetDateTime(this.ruleForm.scekillTime[1], 'isScpe')) + Number(this.fnGetDateTime(this.ruleForm.scekillData, 'date')) - nowTime,
                time = [];

            let showStart = this.setDate(new Date(start).getHours()) + ':' + this.setDate( new Date(start).getMinutes()),
                showEnd = this.setDate(new Date(ent).getHours()) + ':' + this.setDate(new Date(ent).getMinutes());

            time.push(start, ent);

            this.createTimeList.push({
                value: time,
                label: showStart + '-' + showEnd
            });
        },

        /**
         * 移除秒杀时间
         */
        handleClose (tag,index) {
            this.createTimeList.splice(this.createTimeList.indexOf(tag), 1);
        },

        /**
         * 确定排入秒杀
         */
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.createTimeList.map(item=>{
                        this.$post('/student/shop_goods_flash_sale%5Cadd_time', {
                            start_time: item.value[0] / 1000,
                            end_time: item.value[1] / 1000
                        }, resp=> {
                            if (resp.code == 1) {
                                this.indexSucess ++;
                            } else if (resp.code == 0) {
                                this.indexError ++;
                            }
                        });
                    });
                    this.dialogVisible = !this.dialogVisible;

                    setTimeout(()=>{
                        if (this.indexError == 0) {
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            });
                        } else {
                            this.$message({
                                type: 'waring',
                                message: `创建失败个数${this.indexError}个`
                            });
                        }
                        this.indexSucess = this.indexError = 0;
                    },500);

                    setTimeout(()=>{
                        this.getTableData();
                    },500);

                } else {
                    return false;
                }
            });
        },

        /**
         * 取消排入秒杀
         */
        resetForm(formName) {
            this.dialogVisible = !this.dialogVisible;
            this.$refs[formName].resetFields();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        overflow: hidden;
        .header {
            margin-bottom: 20px;
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            // justify-content: space-between;
            color: #606266;
            .header-box {
                display: flex;
                align-items: center;
                margin-right: 20px;
                margin-bottom: 15px;
            }
            .header-title {
                margin-right: 10px;
                white-space: nowrap;
            }
            .header-right {
                display: flex;
                .el-button {
                    margin-left: 20px;
                }
            }
            .header-boxTime {
                border: 1px solid #dcdfe6;
                border-right: none;
                display: flex;
                align-items: center;
                margin-bottom: 15px;
                margin-right: 20px;
                min-width: 765px;
            }
            .header-data {
                height: 32px;
                display: flex;
                align-items: center;
                transition: all .3s cubic-bezier(.645,.045,.355,1);
                cursor: pointer;
                font-size: 14px;
                color: #606266;
                border-right: 1px solid #dcdfe6;
                .header-current-time {
                    display: block;
                    width: 100%;
                    height: 100%;
                    line-height: 32px;
                    text-align: center;
                    background: #409eff;
                    color: #fff;
                    padding: 0 20px;
                }
                .header-time {
                    display: block;
                    width: 100%;
                    height: 100%;
                    line-height: 32px;
                    text-align: center;
                    padding: 0 20px;
                    white-space: nowrap;
                }
            }
        }
        .el-table-time {
            margin-bottom: 5px;
        }
        .sort-wrap {
            display: flex;
            align-items: center;
            .btn-sort {
                font-size: 14px;
                line-height: 1;
                color: #ccc;
                &:hover {
                    color: #20a0ff;
                }
            }
            .el-icon-edit {
                font-size: 16px;
                margin-left: 5px;
            }
            .sort-time {
                cursor: pointer;
                color: #20a0ff;
                margin-left: 15px;
            }
        }
        .scekillrel {
            margin-right: 156px;
            margin-left: -20px;
            margin-top: 20px;
        }
        .table-list {
            width: 100%;
            // flex: 1;
            overflow-y: auto;
        }
        .table-header {
            padding-top: 5px;
        }
        .tabledata {
            margin-bottom: 50px;
        }
        .curse-add {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .el-cursor-jump {
            color: #20a0ff;
            cursor: pointer;
        }
        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
    .switch-fixed {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        right: 0;
        margin: auto;
        z-index: 999;
        cursor: pointer;
    }
    .el-seckill {
        display: flex;
        flex-wrap: wrap;
        max-height: 150px;
        overflow-y: auto;
        box-sizing: border-box;
        &::-webkit-scrollbar {
            width: 4px;
        }
        .el-tag {
            margin-right: 15px;
            margin-bottom: 15px;
        }
    }
    .el-deduction-list {
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        white-space: nowrap;
        overflow: hidden;
        .el-deduction-left {
            min-width: 30px;
        }
        .el-deduction-ipt {
            width: 100px;
        }
        .el-max-width {
            max-width: 200px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            padding-left: 5px;
        }
    }
    .el-deductionSave {
        margin-left: 305px;
    }
    .img-cover {
        width: 60px;
        height: 60px;
    }
    .empty-wrap {
        text-align: center;
        margin-top: 150px;
        .empty-img {
        width: 400px;
        height: 300px;
        margin: 0 auto;
        background-image: url(//cdn.zsdx.cn/wei/images/hire/manage/market/empty-good-list.png);
        background-position: center;
        background-size: contain;
        background-repeat: no-repeat;
        }
        .empty-text {
        text-align: center;
        margin-top: 40px;
        }
    }
</style>
<style lang="less">
    .containerAdmin .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .containerAdmin .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .containerAdmin .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .containerAdmin .el-dialog__body {
        padding-bottom: 10px;
    }
    .containerAdmin .el-range-editor--small .el-range-separator {
        padding: 0px;
    }
    .containerAdmin {
        .el-provide-text {
            border: none;
            width: 100%;
            box-sizing: border-box;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
            &:hover {
                background: transparent;
            }
        }
    }
    // .containerAdmin .el-dialog {
    //     width: 700px !important;
    // }
</style>